/**
 * 
 */
package com.dycn.posdashboard.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import com.chenlb.mmseg4j.analysis.ComplexAnalyzer;
import com.dycn.posdashboard.listener.SystemInitListener;

/**
 * @author gongqiyi
 *
 */
public class LuceneTextIndexUtils {
	
    protected static final Log log = LogFactory.getLog("com.yihaodian.bi.metaapp.util.LuceneIndexUtils");
    
    public static String INDEX_TERM_PREFIX_STR="0";//检索的内容段的前置字符,用于Lucene的前置*号模糊匹配查询
	
    public static Analyzer getDefaultAnalyzer(){
    	//return new StandardAnalyzer(Version.LUCENE_44);// 标准分词器
    	return new ComplexAnalyzer();// 标准分词器
    	//return new IKAnalyzer();// 标准分词器
    }
    
    /**
     * 
     * @return
     */
    public static String getModelDatasIndexPath(){
    	//return "A";
    	//System.out.println(SystemInitListener.FULLSEARCH_INDEX_FOLDER+File.separator+"_datas_");
    	return SystemInitListener.getFullSearchIndexFolder()+File.separator+"_datas_";
    }
    
    /**
     * 
     * @return
     */
    public static String getModelsIndexPath(){
    	//return "B";
    	return SystemInitListener.getFullSearchIndexFolder()+File.separator+"_models_";
    }
    
    public  static IndexWriter openIndexWriter(String indexFilePath) throws IOException {   
	   Analyzer analyzer=getDefaultAnalyzer();//标准分词器
       IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44,    
    		   analyzer);    
       //增量型索引 ，这个地方是关于你创建索引是增量的还是始终创建的。一般来说，选择create_or_append比较好，无论你是否已经创建了索引，都满足  
       iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
       File folderFile=new File(indexFilePath);
       //log.info(folderFile.getPath());
       if(!folderFile.exists()){
    	   //log.info(folderFile.getPath()+" not exists ");
    	   folderFile.mkdirs();
       }
       IndexWriter indexWriter=new IndexWriter(FSDirectory.open(folderFile), iwc);  
       return indexWriter;
   }
    
	 /**  
	 * 添加到索引库中  
	 * @param list  
	 * @throws IOException  
	 */    
    public static void createIndex(List<Document> list,IndexWriter indexWriter) throws IOException {    
		long start1 = new Date().getTime();  
		//IndexWriter writer = openIndexWriter(indexFilePath);                        
		try {
			for(Document doc : list){
				
				indexWriter.addDocument(doc); 
				//indexWriter.updateDocument(term, doc)
				//indexWriter.updateDocument(new Term("id", doc.getField("id").stringValue()), doc);  
		        //这两句一定要执行，不然的话，更新不会马上执行。  
			}    
		} finally{
			//writer.close();
		}    
		long end1 = new Date().getTime();  
		log.info("创建索引花费时间：" + (double) (end1 - start1) / 1000 + "秒");
	} 
	
    /**
     * 把关键字按空格分隔后,每一组中的中文与非中文独立取出
     * @param orginalKeyWords
     * @return
     */
    public static List<String> getKeyWords(String orginalKeyWords){
    	String[] keyWordArray=orginalKeyWords.trim().split(" ");
    	log.info("输入的原始查询关键字:"+orginalKeyWords);
		String[] tempArray=new String[2];
		List<String> resultList=new ArrayList<String>();
		for(int i=0;i<keyWordArray.length;i++){
			tempArray=CharUtil.getSplitStrByChineseChar(keyWordArray[i]);
			if(tempArray[0]!=null&&!tempArray[0].trim().equals("")) resultList.add(tempArray[0]);
			if(tempArray[1]!=null&&!tempArray[1].trim().equals("")) resultList.add(tempArray[1]);
		}
		
		return resultList;
    }
}
